package top.sssd.auth.controller.userRole;


import java.time.LocalDateTime;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import top.sssd.auth.common.CommonResponse;
import top.sssd.auth.model.userRole.request.UserRoleQueryReq;
import top.sssd.auth.model.userRole.request.UserRoleSaveOrUpdateReq;
import top.sssd.auth.model.userRole.response.UserRoleResp;
import top.sssd.auth.service.userRole.UserRoleService;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;


@Api(tags = "用户角色关联表管理")
@RestController
@RequestMapping("/userrole")
public class UserRoleController {

    @Resource
    private UserRoleService userRoleService;

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询用户角色关联表", httpMethod = "GET")
    public CommonResponse<UserRoleResp> getById(@PathVariable Serializable id) {
        return CommonResponse.ok(userRoleService.getUserRoleRespById(id));
    }

    @GetMapping
    @ApiOperation(value = "分页条件查询用户角色关联表", httpMethod = "GET")
    public CommonResponse<Page<UserRoleResp>> page(UserRoleQueryReq userRoleQueryReq) {
        return CommonResponse.ok(userRoleService.queryPage(userRoleQueryReq));
    }

    @PostMapping
    @ApiOperation(value = "新增一条数据到用户角色关联表", httpMethod = "POST")
    public CommonResponse<Boolean> save(@RequestBody UserRoleSaveOrUpdateReq userRoleSaveOrUpdateReq) {
        Boolean successFlag = userRoleService.saveUserRoleReq(userRoleSaveOrUpdateReq);
        return CommonResponse.ok(successFlag, successFlag ? "success" : "fail");
    }

    @PutMapping
    @ApiOperation(value = "更新一条数据到用户角色关联表", httpMethod = "PUT")
    public CommonResponse<Boolean> update(@RequestBody UserRoleSaveOrUpdateReq userRoleSaveOrUpdateReq) {
        Boolean successFlag = userRoleService.updateUserRoleReq(userRoleSaveOrUpdateReq);
        return CommonResponse.ok(successFlag, successFlag ? "success" : "fail");
    }

    @PatchMapping
    @ApiOperation(value = "更新部分数据到用户角色关联表", httpMethod = "PATCH")
    public CommonResponse<Boolean> patch(@RequestBody UserRoleSaveOrUpdateReq userRoleSaveOrUpdateReq) {
        Boolean successFlag = userRoleService.updateUserRoleReq(userRoleSaveOrUpdateReq);
        return CommonResponse.ok(successFlag, successFlag ? "success" : "fail");
    }

    @DeleteMapping("/{ids}")
    @ApiOperation(value = "根据多个ID删除用户角色关联表", httpMethod = "DELETE")
    public CommonResponse<Boolean> delete(@PathVariable List<Long> ids) {
        Boolean successFlag = userRoleService.removeByIds(ids);
        return CommonResponse.ok(successFlag, successFlag ? "success" : "fail");
    }
}
